//
//  ServerDefault.swift
//  API
//
//  Created by ET|冰琳 on 2017/3/24.
//  Copyright © 2017年 IB. All rights reserved.
//

import Foundation
import ReactiveSwift
import Alamofire


/// 鉴于APP可能使用多个服务器
/// 每一个服务器返回的数据结构可能不一致
/// 故可对每一个服务器建立一个单独的request 并对结果进行解析，以保证最终在开发过程中需要的内容仅为需要的内容
///
/// 对应server的各种path可置于对应server的文件中
///
/// eg: {"msg": "suceed", "code": 0, "data": ... }       //假设调用处仅关心data
/// or eg: {"message" : "", "error": 1, "result": ... }  //假设调用处仅关心result
/// 则可在对应的server中处理下数据 ，仅传递上述关心的字段数据



// MARK: -
extension Server {
    struct Default { }
}

extension Server.Default {
    
    fileprivate static let product = Server(scheme: "http", host: "api.github.com", basePath: "")
    fileprivate static let develop = Server(scheme: "http", host: "api.github.com", basePath: "")
    
    static var host: Server {
        #if DEBUG
            return self.develop
        #else
            return self.product
        #endif
    }
}

func request(
    server: Server = Server.Default.host,
    path: Path,
    method: Alamofire.HTTPMethod = .get,
    parameters: Parameters? = nil,
    encoding: ParameterEncoding = URLEncoding.default,
    headers: HTTPHeaders? = nil)
    -> SignalProducer<Any, NSError>
{
    let urlO = server.url(path)
    guard let url = urlO else {
        fatalError("please provide a right url")
    }
    return API.request(url, method: method, parameters: parameters, encoding: encoding, headers: headers).reactive.responseJSON()
}
